package com.meihua.shellapp.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.aspsine.irecyclerview.IRecyclerView;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.SearchActivity;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.adapter.HomeRVAdapter;
import com.meihua.shellapp.adapter.OnItemClickListener;
import com.meihua.shellapp.adapter.ZhongAnHomeRVAdapter;
import com.meihua.shellapp.base.BaseRecyclerViewFragment1;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.HomeIndexBean;
import com.meihua.shellapp.bean.ZhongAnBean;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.URL;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;


public class HomeFragment extends BaseRecyclerViewFragment1 implements OnItemClickListener {

    @Bind(R.id.top_rl_search)
    RelativeLayout mRlSearch;
    @Bind(R.id.ins_iRecyclerView)
    IRecyclerView mRecyclerView;
    private List<ZhongAnBean.DataBean> mData;
    //众安产品接口适配器
    private ZhongAnHomeRVAdapter rvAdapter;
    private String id = "";
    private SliderLayout mSlider;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_home;
    }

    /**
     * 初始化界面View
     */
    @Override
    protected void initView() {
        mRlSearch.setVisibility(View.VISIBLE);
        if (null != getArguments()) {
            id = getArguments().getString(Constant.FRAGMENT_URL);
        }
        initRecyclerView(mRecyclerView);
        initBannerView();
        showLoadingDialog();
    }

    /**
     * 初始化bannerView
     */
    private void initBannerView() {
        LinearLayout bannerView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.layout_banner_view, mRecyclerView.getHeaderContainer(), false);
        mSlider = bannerView.findViewById(R.id.binnerview);
        bannerTv = bannerView.findViewById(R.id.banner_tv);
        bannerView.setVisibility(id.equals("") ? View.VISIBLE : View.GONE);
        bannerTv.setVisibility(View.VISIBLE);
        //内部图片切换的时候,使用的动画类型
//            mSlider.setPresetTransformer(SliderLayout.Transformer.RotateDown);
        //设置可以轮播容器内部点的所在位置
        mSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        //文本描述出现的时候,指定的动画
        mSlider.setCustomAnimation(new DescriptionAnimation());
        //动画的执行时长
//        setBannerView(mSlider,BANNER_URL);
        mSlider.setDuration(3000);
        mRecyclerView.addHeaderView(bannerView);
    }

    /**
     * 设置adapter
     *
     * @return
     */
    @Override
    protected RecyclerView.Adapter getAdapter() {
        mData = new ArrayList<>();
        rvAdapter = new ZhongAnHomeRVAdapter(mData);
        rvAdapter.setOnItemClickListener(this);
        return rvAdapter;
    }

    /*请求服务器数据*/
    @Override
    public void getServiceData(final int page) {
//        mData.clear();
        if (mPage == 1) setBannerView(mSlider, URL.getUrl().BANNER_URL);
//        dismissLoadingDialog();
//        mData.addAll(addDataForBoss(null));
//        rvAdapter.notifyDataSetChanged();
//        Map<String, String> params = new HashMap() {{
//            put("page", page + "");
//            put("name", "");
//            put("type_id", id);
//        }};
//        NetHelper.post(URL.getUrl().HOME_URL, params, new NetHelper.HttpCallBack() {
//            @Override
//            public void onSuccess(Response<String> response) {
//                dismissLoadingDialog();
//                HomeIndexBean bean = MyApplication.getInstance().getBeanForJson(HomeIndexBean.class, response.body());
//                if (bean == null) return;
//                if (null != bean.getData()) {
//                    if (bean.getData().size() != 0) {
//                        if (mPage == 1) {
//                            /*刷新*/
//                            if (mRecyclerView != null) {
//                                mRecyclerView.setRefreshing(false);
//                                mData.clear();
//                            }
//                        }
//                        /*添加学落单说的数据*/
//                        mData.addAll(addDataForBoss(bean.getData()));
//                        rvAdapter.notifyDataSetChanged();
//                    } else {
//                        mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
//                    }
//                } else {
//                    mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
//                }
//            }
//
//        }, true);
        //众安产品接口
        NetHelper.post(URL.getUrl().ZHONGAN_URL, new HashMap<String, String>(), new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                dismissLoadingDialog();
                ZhongAnBean bean = MyApplication.getInstance().getBeanForJson(ZhongAnBean.class, response.body());
                if (bean == null) return;
                if (null != bean.getData()) {
                    if (bean.getData().size() != 0) {
                        if (mPage == 1) {
                            /*刷新*/
                            if (mRecyclerView != null) {
                                mRecyclerView.setRefreshing(false);
                                mData.clear();
                            }
                        }
                        /*添加学落单说的数据*/
                        mData.addAll(bean.getData());
                        rvAdapter.notifyDataSetChanged();
                    } else {
                        //mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                    }
                } else {
                    //mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                }
            }

        }, true);
    }

    private Collection<? extends HomeIndexBean.DataBean> addDataForBoss(List<HomeIndexBean.DataBean> data) {
        List<HomeIndexBean.DataBean> tempData = new ArrayList<>();
        HomeIndexBean.DataBean dataBean1 = new HomeIndexBean.DataBean();
        dataBean1.setId("23333");
        dataBean1.setProduct_name("尊享e生医疗保险（50万版）");
        dataBean1.setPremium("99");//价格
        dataBean1.setFit_member("30-60周岁");//	适合人群
        dataBean1.setFeilv(new HomeIndexBean.DataBean.FeilvBean("30", "60", "一年"));//价格
        tempData.add(dataBean1);

        HomeIndexBean.DataBean dataBean2 = new HomeIndexBean.DataBean();
        dataBean2.setId("23333");
        dataBean2.setProduct_name("尊享无忧住院医疗");
        dataBean2.setPremium("99");//价格
        dataBean2.setFit_member("30-60周岁");//	适合人群
        dataBean2.setFeilv(new HomeIndexBean.DataBean.FeilvBean("30", "60", "一年"));//价格
        tempData.add(dataBean2);

        HomeIndexBean.DataBean dataBean3 = new HomeIndexBean.DataBean();
        dataBean3.setId("23333");
        dataBean3.setProduct_name("癌症医疗保险");
        dataBean3.setPremium("99");//价格
        dataBean3.setFit_member("30-60周岁");//	适合人群
        dataBean3.setFeilv(new HomeIndexBean.DataBean.FeilvBean("30", "60", "一年"));//价格
        tempData.add(dataBean3);

        HomeIndexBean.DataBean dataBean4 = new HomeIndexBean.DataBean();
        dataBean4.setId("23333");
        dataBean4.setProduct_name("个人综合意外保险");
        dataBean4.setPremium("99");//价格
        dataBean4.setFit_member("30-60周岁");//	适合人群
        dataBean4.setFeilv(new HomeIndexBean.DataBean.FeilvBean("30", "60", "一年"));//价格
        tempData.add(dataBean4);

        HomeIndexBean.DataBean dataBean5 = new HomeIndexBean.DataBean();
        dataBean5.setId("23333");
        dataBean5.setProduct_name("糖尿病并发症保险");
        dataBean5.setPremium("99");//价格
        dataBean5.setFit_member("30-60周岁");//	适合人群
        dataBean5.setFeilv(new HomeIndexBean.DataBean.FeilvBean("30", "60", "一年"));//价格
        tempData.add(dataBean5);

        HomeIndexBean.DataBean dataBean6 = new HomeIndexBean.DataBean();
        dataBean6.setId("23333");
        dataBean6.setProduct_name("尊享e生医疗保险（100万版）");
        dataBean6.setPremium("99");//价格
        dataBean6.setFit_member("30-60周岁");//	适合人群
        dataBean6.setFeilv(new HomeIndexBean.DataBean.FeilvBean("30", "60", "一年"));//价格
        tempData.add(dataBean6);

        HomeIndexBean.DataBean dataBean7 = new HomeIndexBean.DataBean();
        dataBean7.setId("23333");
        dataBean7.setProduct_name("孝欣保 老年综合意外险");
        dataBean7.setPremium("99");//价格
        dataBean7.setFit_member("30-60周岁");//	适合人群
        dataBean7.setFeilv(new HomeIndexBean.DataBean.FeilvBean("30", "60", "一年"));//价格
        tempData.add(dataBean7);
//        tempData.addAll(data);
        return tempData;
    }

    /*条目点击*/
    @Override
    public void onItemClick(int position, View v) {
        boolean isLogin = SharedPreferencesUtil.getInstance().getBoolean(getContext(),Constant.ISLOGIN,false);
//        if (!isLogin){
//            Intent intent = new Intent(getActivity(), LoginActivity.class);
//            startActivity(intent);
//            return;
//        }
        Intent intent = new Intent(getActivity(), WebActivity.class);
//        String url = URL.getUrl().WEBPRODUCT + mData.get(position).alian + "/" + MyApplication.getInstance().getMId();
//        switch (position){
//            case 0:
//                url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000688&accountId=502325089";
//                break;
//            case 1:
//                url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000692&accountId=502325089";
//                break;
//            case 2:
//                url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000689&accountId=502325089";
//                break;
//            case 3:
//                url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000687&accountId=502325089";
//                break;
//            case 4:
//                url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000690&accountId=502325089";
//                break;
//            case 5:
//                url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000697&accountId=502325089";
//                break;
//            case 6:
//                url = "https://sky.zhongan.com/open/qrcode/iybQrcodeProduct?productId=1000695&accountId=502325089";
//                break;
//            default:
//                break;
//        }
        intent.putExtra("url", mData.get(position).alian);
        startActivity(intent);
    }

    /*实例化*/

    public static HomeFragment newInstance(String id) {
        HomeFragment mInstance = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(Constant.FRAGMENT_URL, id);
        mInstance.setArguments(args);
        return mInstance;
    }

    /**
     * 点击事件
     */
    @OnClick(R.id.top_rl_search)
    public void onViewClicked() {
        /*搜索中心界面*/
        startActivity(new Intent(getActivity(), SearchActivity.class));
    }
}